package scau.erps.util;


/**
 * 时间计算器
 * @author DCJ
 * 
 */
public class CalculateQuarter {
	
	/**
	 * 计算账期为1Q的时间
	 * @param beginQuarter
	 * @return
	 * 修改者 郑剑川 修改了addOnequarter.
	 * year++;   改成year++;quarter=1;
	 */
	public static String addOneQuarter(String beginQuarter){
		String endQuarter = null;
		String strs[] = beginQuarter.split("\\D");
		Integer year = Integer.parseInt(strs[1]);
		Integer quarter = Integer.parseInt(strs[2]);
		if (quarter==4){
			year++;
			quarter=1;
		} else{
			quarter++;
		}
		endQuarter = "Y"+year.toString()+"Q"+quarter.toString();
		return endQuarter;
	}
	
	/**
	 * 计算账期为2Q的时间
	 * @param beginQuarter
	 * @return
	 */
	public static String addTwoQuarter(String beginQuarter){
		String endQuarter = null;
		String strs[] = beginQuarter.split("\\D");
		Integer year = Integer.parseInt(strs[1]);
		Integer quarter = Integer.parseInt(strs[2]);
		if (quarter==1 || quarter==2){
			quarter = quarter+2;				
		} 
		else if(quarter==3){
			year++;
			quarter = 1;
		}
		else if(quarter==4){
			year++;
			quarter = 2;
		}
		endQuarter = "Y"+year.toString()+"Q"+quarter.toString();
		return endQuarter;
	}
	
	/**
	 * 计算账期为3Q的时间
	 * @param beginQuarter
	 * @return
	 */
	public static String addThreeQuarter(String beginQuarter){
		String endQuarter = null;
		String strs[] = beginQuarter.split("\\D");
		Integer year = Integer.parseInt(strs[1]);
		Integer quarter = Integer.parseInt(strs[2]);
		switch (quarter) {
		case 1:
			quarter = 4;
			break;
		case 2:
			year++;
			quarter = 1;
			break;
		case 3:
			year++;
			quarter = 2;
			break;
		case 4:
			year++;
			quarter = 3;
			break;
		}
		endQuarter = "Y"+year.toString()+"Q"+quarter.toString();
		return endQuarter;
	}

		
	/**
	 * 计算账期为4Q的时间
	 * @param beginQuarter
	 * @return
	 */
	public static String addFourQuarter(String beginQuarter){
		String endQuarter = null;
		String strs[] = beginQuarter.split("\\D");
		Integer year = Integer.parseInt(strs[1]);
		year++;
		endQuarter = "Y"+year.toString()+"Q"+strs[2];
		return endQuarter;
	}
	/**
	 * 计算加n年的时间
	 * @param beginQuarter
	 * @return
	 */
	public static String addYears(String beginQuarter,int year){
		String endQuarter = null;
		String strs[] = beginQuarter.split("\\D");
		Integer endYear = Integer.parseInt(strs[1])+year;
		endQuarter = "Y"+endYear+"Q"+strs[2];
		return endQuarter;
	}
	
	/**
	 * 计算申请时间与当前时间差值
	 * 		eg 当前时间Y3Q2,贷款申请时间Y2Q3, 求位于沙盘的？Q上
	 * @param currentQuarter
	 * @param applyQuarter
	 * @return
	 */
	public static int getQuarter(String currentQuarter, String applyQuarter){
		int id=0;		
		if (currentQuarter.equals(addOneQuarter(applyQuarter))){
			id=1;			
		}
		else if(currentQuarter.equals(addTwoQuarter(applyQuarter))){
			id=2;
		}
		else if(currentQuarter.equals(addThreeQuarter(applyQuarter))){
			id=3;
		}
		else if(currentQuarter.equals(addFourQuarter(applyQuarter))){
			id=4;
		}
		return id;
	}
	
	/**
	 * 计算结束时间与当前时间年差
	 * @param currentQuarter
	 * @param endQuarter
	 * @return
	 */
	public static int getYear(String currentQuarter, String endQuarter){
		String current[] = currentQuarter.split("\\D");
		String end[] = endQuarter.split("\\D");		
		int a = Integer.parseInt(current[1]);
		int b = Integer.parseInt(end[1]);		
		return b-a;
	}
	
	/**
	 * 计算当前时间向后推移1Q~4Q结果
	 * @author outsider
	 * 
	 */
	public static String calculateQuarter(String beginQuarter,String quarter){
		String endQuarter = null;
		if (quarter.equals("1Q")) return addOneQuarter(beginQuarter);
		if (quarter.equals("2Q")) return addTwoQuarter(beginQuarter);
		if (quarter.equals("3Q")) return addThreeQuarter(beginQuarter);
		if (quarter.equals("4Q")) return addFourQuarter(beginQuarter);
		return endQuarter;
	}
	
	/**
	 * 得到当前季度
	 * @param time
	 * @return
	 */
	public static int getCurrentQuarter(String time){
		String strs[] = time.split("\\D");
		Integer i = Integer.parseInt(strs[2]);		
		return i;		
	}
	/**
	 * 得到当前年
	 * @param time
	 * @return
	 */
	public static String getCurrentYear(String time){
		String year = null;
		String strs[] = time.split("\\D");
		Integer i = Integer.parseInt(strs[1]);
		year = "Y"+i;
		return year;		
	}
	public static int getCurrentYearInt(String time){
		String strs[] = time.split("\\D");
		Integer i = Integer.parseInt(strs[1]);
		return i;	
	}
	/**
	 * 得到下一年
	 * @param time
	 * @return
	 */
	public static String getNextYear(String time){
		String year = null;
		String strs[] = time.split("\\D");
		Integer i = Integer.parseInt(strs[1])+1;
		year = "Y"+i;
		return year;
	}
	/**
	 * 得到上一年
	 * @param time
	 * @return
	 */
	public static String getLastYear(String time){
		String year = null;
		String strs[] = time.split("\\D");
		Integer i = Integer.parseInt(strs[1])-1;
		year = "Y"+i;
		return year;
	}
	/**
	 * 获取生产线完工时间
	 * @param time
	 * @return
	 */
	public static String getLineCompleteTime(String time,String type){
		if (type.equals("手工线")) return CalculateQuarter.addOneQuarter(time);
		if (type.equals("半自动")) return CalculateQuarter.addTwoQuarter(time);
		if (type.equals("全自动")) return CalculateQuarter.addThreeQuarter(time);
		return CalculateQuarter.addFourQuarter(time);
	}
	/**
	 * 获取产品完工时间
	 * @param time
	 * @return
	 */
	public static String getProductCompleteTime(String time,String type){
		if (type.equals("手工线")) return CalculateQuarter.addThreeQuarter(time);
		if (type.equals("半自动")) return CalculateQuarter.addTwoQuarter(time);
		if (type.equals("全自动")) return CalculateQuarter.addOneQuarter(time);
		return CalculateQuarter.addOneQuarter(time);
	}
}
